<template>
  <div class="form-sparepart">
    <fault-table 
      :data="faultData" 
      :task="task" 
      isReceipt
      @dataChange="setData"
      :addFaultAuth="true"
      :deleteFaultBtn="true"
      :editFaultBtn="true"
    />
  </div>
</template>

<script>
import { isOpenData } from '@src/util/platform'

/* components */
import FaultTable from '@src/modules/task/editAllot/components/PartSeviceList/components/FaultTable.vue';
/* mixin */
import FormMixin from '@src/component/form/mixin/form';


/* util */
import _ from 'lodash';
import { getRootWindow } from '@src/util/dom'
export default {
  name: 'form-new-fault-library',
  mixins: [FormMixin],
  inject: ['initData'],
  props: {
    field: {
      type: Object,
      default: () => ({})
    },
    value: [String, Number, Array],
  },
  data() {
    return {
      faultData: [],
      task: {},
      tableAllData: []
    }
  },
  created() {
    const { task = {}, auth } = this.initData;
    this.task = task;
  },
  watch: {
    value: {
      handler(newV, oldV) {
        this.tableAllData = newV || [];
      },
      deep: true
    }
  },
  methods: {
    // 列表获取
    async getTaskFaultTableData(){
      try {
        const { task = {}, auth } = this.initData;
        this.task = task;
        
        const res = await getTaskFaultCardSelect({
          taskId: task.id
        });
        if(!MsgModel.isSuccess(res)) return message.error(res.message);

        // 展示数据处理
        this.tableAllData = (res.data || []).map(x => {
          const { solutionVO = {}, objDesc, taskId, taskNo , solutionId, faultSource, id, faultDesc, faultId } = x || {};

          return {
            id,
            solutionDesc: solutionVO?.solutionDesc || '',
            taskId,
            taskNo,
            solutionId,
            faultId,
            parentId: solutionVO?.libraryId || '',
            faultDesc,
            isTree: solutionVO?.solutionSource === 0 || faultSource === 0,
            objDesc
          }
        })
      }catch(e) {
        console.log('getTaskFaultCardSelect: ', e)
      }
    },
    // 设置数据
    setData(data) {
      this.faultData = data || [];
      this.$emit('update', {field: this.field, newValue: this.faultData});
    },
  },
  components: {
    [FaultTable.name]: FaultTable
  }
}
</script>

<style lang="scss" scope>

</style>
